package com.dyava.commons.validate;


import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;

import java.util.Collection;

public class Validate<T,V> {
	protected boolean relax;
	protected T value;
	protected String name;


	protected boolean checkNull(){
		if(relax && value==null){
			return true;
		}
		notNull();
		return false;
	}

	public V orNull(){
		relax = true;
		return (V)this;
	}

	public V notNull(){
		if(this.value !=null){
			return (V)this;
		}
		throw new ValidateFastException(name+"必须不为空" );
	}
	public T value(){
		return value;
	}
	public T value(T t){
		if(value==null){
			return t;
		}
		return value;
	}

	public V eq(T value){
		if (checkNull()) return (V)this;
		if(this.value.equals(value)){
			return (V)this;
		}
		throw new ValidateFastException(name+"必须等于"+value);
	}

	public V ne(T value){
		if (checkNull()) return (V)this;
		if(!this.value.equals(value)){
			return (V)this;
		}
		throw new ValidateFastException(name+"必须不等于"+value);
	}

	public V in(T[] values){
		if (checkNull()) return (V)this;
		if(ArrayUtils.contains(values,this.value)){
			return (V)this;
		}
		throw new ValidateFastException(name+"参数错误");
	}

	public V in(Collection<T> values){
		if (checkNull()) return (V)this;
		if(CollectionUtils.containsAny(values,this.value)){
			return (V)this;
		}
		throw new ValidateFastException(name+"参数错误");
	}


}
